package cj.web.admin.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import cj.web.admin.dao.ConfigInfoDAO;
import cj.web.admin.domain.ConfigInfo;
import cj.web.point.dao.PointConfigDAO;
import cj.web.point.domain.PointConfig;
/** 
 * @author ROC 
 * 2010年4月5日18:26:55 
 * 实现getAllInfo方法，取得所有常用字典表里的数据 
 *  
 * */  
public class GetDictionaryInfo {  
	
	/**
	 * https://blog.csdn.net/peng_wu01/article/details/5453361
	 * 
	 * reloadDictionaryInfoMap();实现起来时比较困难的，要在GetDictionaryInfo 取到ServletContext，
	 * 而且要必须取到第一次实例的GetDictionaryInfo ，所有有一个东西很好用，就是Spring的context,取到他的方式
	 * 
	 * 刷新
	 * GetDictionaryInfo.getInstance().reloadDictionaryInfoMap();  
	 * 
	 * 赋值
	 * private static WebApplicationContext springContext;  
	 * springContext = WebApplicationContextUtils.getWebApplicationContext(_servletContext);  
	 */
	
	@Autowired
    private ConfigInfoDAO configInfoDAO;
	@Autowired
    private PointConfigDAO pointConfigDAO;
      
    public GetDictionaryInfo(){  
        super();  
    }  
    private static WebApplicationContext springContext;//这个是为了取springContext  
    public static ServletContext _servletContext;//这个是为了取servletContext  
    private static GetDictionaryInfo _instance;   
    public static HashMap<String,Object> dictionaryInfoMap = new HashMap<String, Object>();  
    
    public static GetDictionaryInfo getInstance() {  
        springContext = WebApplicationContextUtils  
        .getWebApplicationContext(_servletContext);  
        if(null == _instance)  
            _instance = (GetDictionaryInfo)springContext.getBean("getDictionaryInfo");  
        return _instance;  
          
    }  
    
    public  void getAllInfo() throws NumberFormatException, Exception {  
        System.out.println("%%%%%%%%%%系统启动加载......");  
        List<ConfigInfo> configlis = configInfoDAO.queryAll();
        Map configRep = new HashMap();
		for(ConfigInfo rep:configlis){
			configRep.put(rep.getCode(), rep.getValue());
		}
		
		List<PointConfig> pointConfiglis = pointConfigDAO.queryAll();
        Map pointConfigRep = new HashMap();
        Map pointConfigPointNum = new HashMap();
        
		for(PointConfig pointConfig:pointConfiglis){
			String categoryId = pointConfig.getCategoryId();
			List<Map> pointlis = new ArrayList<Map>();
			for(int i=0;i<categoryId.split(",").length;i++){
				 Map pointrep = new HashMap();
				 pointrep.put("categoryId", categoryId.split(",")[i].toString());
				 pointlis.add(pointrep);
			}
			
			pointConfigRep.put(pointConfig.getCode(), pointlis);
			pointConfigPointNum.put(pointConfig.getCode(), pointConfig.getPointNum());
		}
		
		dictionaryInfoMap.put("configRep", configRep);
		dictionaryInfoMap.put("pointConfigRep", pointConfigRep);
		dictionaryInfoMap.put("pointConfigPointNum", pointConfigPointNum);
		
        System.out.println("@@@@@@@@@@数据打包完毕，全部放进了dictionaryInfoMap！");  
    }  
    
    /** 
     * 重新加载字典表数据！ 
     * @throws Exception 
     * @throws NumberFormatException 
     * */  
    public void reloadDictionaryInfoMap() throws NumberFormatException, Exception {  
        System.out.println("==========~~~~~~~~~~系统开始重新加载字典表");  
        dictionaryInfoMap.clear();  
        System.out.println("==========~~~~~~~~~~字典表MAP,清空成功！");  
        getAllInfo();  
        System.out.println("==========~~~~~~~~~~重新加载字典表成功");  
    }  
      
    public HashMap<String, Object> getDictionaryInfoMap() {  
        return dictionaryInfoMap;  
    }  
    public void setDictionaryInfoMap(HashMap<String, Object> dictionaryInfoMap) {  
        this.dictionaryInfoMap = dictionaryInfoMap;  
    }  
}  